/*
 * Copyright (c) 2012,东方口岸科技有限公司
 * All rights reserved.
 * 文件名称：RouterMessageReceiver.java
 * 摘    要：
 * 版本：1.0
 * 作    者：fengzhiyong
 */
package com.cneport.transfer.router.biz;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cneport.transfer.constant.RouterTypeConstants;
import com.cneport.transfer.message.dtf.DtfCtrl;
import com.cneport.transfer.message.dtf.DtfCtrlRep;
import com.cneport.transfer.message.dtf.DtfCtrl.RouteList.RouteInfo;
import com.cneport.transfer.router.api.IMessageReceiver;
import com.cneport.transfer.router.model.Router;
import com.cneport.transfer.router.support.service.RouterService;
import com.cneport.transfer.router.support.service.SequenceService;
import com.cneport.transfer.util.DateUtil;
import com.cneport.transfer.util.XmlSerializer;
@Component
public class DtfRouterMessageReceiver implements IMessageReceiver {
    /**
     *@CreateDate:2012-7-11
     *@Description:
     */
    private static Logger logger = Logger
            .getLogger(DtfRouterMessageReceiver.class);
    @Autowired
    private SequenceService sequenceService;
    @Autowired
    private RouterService routerService;

    public String routerMessageReceiver(String ctrlMessage) {
        DtfCtrl dtfCtrl = string2Object(ctrlMessage);
        List<Router> routerList = dtfCtrl2Router(dtfCtrl);
        DtfCtrlRep dtfCtrlRep = new DtfCtrlRep();
        dtfCtrlRep.setMsgId(dtfCtrl.getMsgId());
        dtfCtrlRep.setOriMsgId(dtfCtrl.getMsgId());
        if (ctrlMessage2DB(routerList)) {
            dtfCtrlRep.setNote(null);
            dtfCtrlRep.setRepCode(1);
            dtfCtrlRep.setRepInfo("成功");
        } else {
            dtfCtrlRep.setNote(null);
            dtfCtrlRep.setRepCode(-1);
            dtfCtrlRep.setRepInfo("添加删除失败！");
        }
        String dtfCtrlRepMessage;
        try {
            dtfCtrlRepMessage = XmlSerializer.serialize(dtfCtrlRep);
        } catch (Exception e) {
            logger.error("路由控制应答报文序列号错误！");
            return null;
        }
        return dtfCtrlRepMessage;

    }

    public DtfCtrl string2Object(String ctrlMessage) {
        Object obj;
        try {
            obj = XmlSerializer.deSerialize(ctrlMessage, DtfCtrl.class);
            DtfCtrl dtfCtrl = (DtfCtrl) obj;
            return dtfCtrl;
        } catch (Exception e) {
            logger.error("路由控制报文解析错误！");
            return null;
        }

    }

    public List<Router> dtfCtrl2Router(DtfCtrl dtfCtrl) {
        List<Router> routerList = new ArrayList<Router>();
        List<RouteInfo> routerInfoList = dtfCtrl.getRouteList().getRouteInfo();
        for (DtfCtrl.RouteList.RouteInfo routerInfo : routerInfoList) {
            Router router = new Router();
            router.setEndNode(routerInfo.getEndNode());
            router.setRouterEffectTime(DateUtil.convert2Date(dtfCtrl
                    .getRouteList().getEfficientTime()));
            router.setRouterExpression(routerInfo.getNextNode());
            router.setRouterId(sequenceService.getSequence("routerID"));
            router.setRouterType(RouterTypeConstants.DTF_ROUTE);
            router.setRouterVer(dtfCtrl.getRouteList().getVersion());
            router.setStartNode(dtfCtrl.getRecNodeId());
            routerList.add(router);
        }
        return routerList;

    }

    public boolean ctrlMessage2DB(List<Router> routerList) {
        routerService.deleteRouter(RouterTypeConstants.DTF_ROUTE);
        int addRouterNum = routerService.insertRouter(routerList);
        if (routerList.size() == addRouterNum) {
            logger.info("更新路由成功！添加路由记录条数为：" + addRouterNum);
            return true;
        } else {
            logger.error("更新路由失败！添加路由记录条数与应加路由记录条数不服");
            return false;
        }
    }
}
